package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.GoodsBean;
import com.project.mapper.IGoodsMapper;
import com.project.service.IGoodsService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class GoodsServiceImpl extends BaseService implements IGoodsService {
    @Override
    public int add(GoodsBean goods) {
        SqlSession session = this.getSession();
        try {
            IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
            mapper.add(goods);
            //提交事务
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
            session.rollback();
        }finally {
            //关闭session
            session.close();
        }

        return goods.getId();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.del(id);

        session.commit();
        session.close();
    }

    @Override
    public void update(int id,int price) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.update(id,price);

        session.commit();
        session.close();
    }

    @Override
    public void addMore(List<GoodsBean> list) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.addMore(list);

        session.commit();
        session.close();
    }

    @Override
    public void delMore(int[] idArray) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.delMore(idArray);

        session.commit();
        session.close();
    }

    @Override
    public List<GoodsBean> findByBirthday(LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        List<GoodsBean> list = mapper.findByBirthday(startDate,endDate);

        session.close();
        return list;
    }

    @Override
    public CutPageBean<GoodsBean> cutFindItem(int pageNo, String name, String type, int startPrice, int endPrice) {
        CutPageBean<GoodsBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        List<GoodsBean> list =
                mapper.cutItemList((pageNo - 1) * PAGESIZE,PAGESIZE,name,type,startPrice,endPrice);
        List pageArray = list.subList(0,3);
        int count = list.size();
        int totalPage = count % PAGESIZE == 0 ?
                count/PAGESIZE : count/PAGESIZE + 1;
////        //将数据封装为分页对象
        cutBean.setList(pageArray);
        cutBean.setCount(count);
        cutBean.setTotalPage(totalPage);

        session.close();
        return cutBean;
    }
}
